package com.leo;

import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.scene.layout.AnchorPane;
import javafx.stage.Stage;

import java.io.File;
import java.io.FileInputStream;
import java.net.URL;

/**
 * @ClassName ImageDemo
 * @Description
 * @Author wangss
 * @date 2020.06.02 12:57
 * @Version 1.0
 */
public class ImageDemo extends Application {

    public static void main(String[] args) {
        launch(args);
    }

    @Override
    public void start(Stage primaryStage) throws Exception {
        AnchorPane an = new AnchorPane();
        an.setStyle("-fx-background-color: #e7eeea");
        Scene scene = new Scene(an);

        FileInputStream fis = new FileInputStream(new File("leo-051-image-imageview/src/main/resources/images/1.jpg"));
        Image image = new Image(fis);

        FileInputStream fis1 = new FileInputStream(new File("leo-051-image-imageview/src/main/resources/images/1.jpg"));
        // 后两个参数是  缩放比例 和 图片质量
        Image image1 = new Image(fis1, 600d, 600d, false, true);

        // 相对路径
        Image image2 = new Image("images/1.jpg");
        // 绝对路径，需要加 file
        Image image3 = new Image("file:leo-051-image-imageview/src/main/resources/images/1.jpg");

        String path = "images/7.jpeg";
        // 后三个参数为  缩放比例 图片质量 异步加载
        Image image4 = new Image(path, 600d, 600d, true, true, true);

        // 加载网络图片，需要是图片格式结尾
        String httpPath = "https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1591108797634&di=6e70d59a8c0149e523e2683fae57c094&imgtype=0&src=http%3A%2F%2Fa2.att.hudong.com%2F42%2F31%2F01300001320894132989315766618.jpg";
        Image image5 = new Image(httpPath, 600d, 600d, true, true, true);

        // 通过类加载器加载图片
        URL resource = getClass().getClassLoader().getResource("images/1.jpg");
        String urlPath = resource.toExternalForm();
        Image image6 = new Image(urlPath);

        URL resource1 = getClass().getResource("/images/1.jpg");
        String urlPath1 = resource1.toExternalForm();
        Image image7 = new Image(urlPath1);

        ImageView imageView = new ImageView(image7);

        an.getChildren().add(imageView);
        primaryStage.setScene(scene);
        primaryStage.setTitle("ImageViewDemo");
        primaryStage.setWidth(600d);
        primaryStage.setHeight(600d);
        primaryStage.setResizable(false);
        primaryStage.show();
    }
}
